package com.film.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.UserDao;
import com.film.domain.User;
import com.film.rowMapper.UserRowMapper;
import com.film.util.DBUtil;

public class UserDaoImpl implements UserDao{
	
	JdbcTemplate template = new JdbcTemplate(DBUtil.getDataScorse());

	@Override
	public int insertUser(User user) {
		String sql = "insert into user(u_name,u_password,u_phoneNum,u_nickName) values(?,?,?,?)";
		return template.update(sql,user.getuName(),user.getuPassword(),user.getuPhoneNum(),user.getuNickName());
	}

	@Override
	public User selectOne(String username) {
		User user = null;
		String sql = "select * from user where u_name = ?";
		List<User> users = template.query(sql, new UserRowMapper(),username);
		if(users.size() == 1) {
			user = users.get(0);
		}
		return user;
	}

	@Override
	public User selectOneByPhone(String phoneNum) {
		User user = null;
		String sql = "select * from user where u_phoneNum = ?";
		List<User> users = template.query(sql, new UserRowMapper(),phoneNum);
		if(users.size() == 1) {
			user = users.get(0);
		}
		return user;
	}

	@Override
	public User selectOneUser(String username, String password) {
		
		User user = null;
		String sql = "select * from user where u_name =?  and u_password = ?";
		List<User> users = template.query(sql, new UserRowMapper(),username,password);
		if(users.size() == 1) {
			user = users.get(0);
		}
		return user;
	}

	@Override
	public int updateById(User user) {
		
		String sql = "UPDATE user SET u_nickName=? , u_sex=? , u_birthday=? , u_life_State=? , u_industry=? , u_signature=? WHERE u_id=?";
		
		return template.update(sql,user.getuNickName(),user.getuSex(),user.getuBirthday(),user.getuLiftState(),user.getuIndustry(),user.getuSignature(),user.getuId());
	}

	@Override
	public User selectALl(int u_id) {
		User user = null;
		String sql="select * from user where u_id =?";
		List<User> users = template.query(sql, new UserRowMapper(),u_id);
		if(users.size() == 1) {
			user = users.get(0);
		}
		return user;
	}

	@Override
	public int updateImg(int uId, String imgPath) {
		String sql = "UPDATE user SET u_headImg=? where u_id=?";
		return template.update(sql,imgPath,uId);
	}






	@Override
	public List<User> selectALl() {
		List<User> list=new ArrayList<User>();
		String sql = "select * from user";
		list = template.query(sql, new UserRowMapper());
		
		return list;
	}
	@Override
	public int deleteUser(Integer uId) {
		String sql = "DELETE FROM user WHERE u_id = ?";
		return template.update(sql,uId);
	}

	
	
	
	
	
	
	
	
	@Override
	public List<User> selectlimit( int a,int b) {
		String sql="SELECT  * FROM user LIMIT ?,?";
		return template.query(sql,new UserRowMapper(),a,b);
	}
}
